package manager_web.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import manager_service.service.BackChapterService;
import org.omg.CosNaming.NamingContextExtPackage.StringNameHelper;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;
import test_common.constant.Constant;
import test_common.entity.Chapter;
import test_common.utils.ContentIsNull;
import test_common.utils.ResponseResult;

import javax.print.DocFlavor;
import javax.servlet.http.HttpServletRequest;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author zhangzheng
 * @create 2021/1/5
 */
@Controller
@RequestMapping("/chapter")
public class ChapterController {
    @Reference
    private BackChapterService backChapterService;

    @RequestMapping("/addChapter")
    public String  addChapter(HttpServletRequest request){
        String courseId=request.getParameter("course-resource-id");
        String chapterTitle=request.getParameter("course-resource-title");
        String info=request.getParameter("course-resource-context");
//        String resourceChapter=request.getParameter("course-resource-file-title");
//        String costType=request.getParameter("file_cost_type_id");
//        String costNumber=request.getParameter("file-cost-type-val");
//        String fileType=request.getParameter("resource_type_id");
        Date createDate=new Date();
        Integer status= Constant.CHAPTER_ENABLE;
        Chapter chapter=new Chapter();
        chapter.setCourseId(Integer.parseInt(courseId));
        chapter.setTitle(chapterTitle);
        chapter.setInfo(info);
        chapter.setCreateDate(createDate);
        chapter.setStatus(status);
        backChapterService.addChapter(chapter);
        return "/back_courseReourceSet";

    }

    @ResponseBody
    @RequestMapping("/showChapters")
    public ModelAndView showChapters(HttpServletRequest request){
        ModelAndView mav=new ModelAndView();
        String courseId=request.getParameter("courseId");
        String pageNo=request.getParameter("pageNo");
        String pageSize=request.getParameter("pageSize");
        //如果前端没有传递，使用默认值
        if (ContentIsNull.isNull(pageSize)) {
            pageSize= Constant.Chapter_DEFAULT_PAGE_SIZE;
        }
        if (ContentIsNull.isNull(pageNo)){
            pageNo= Constant.Chapter_DEFAULT_PAGE_NO;
        }
        int pageNoint=Integer.parseInt(pageNo);
        int pageSizeint=Integer.parseInt(pageSize);
        PageInfo<Chapter> chapterPageInfo=backChapterService.showChapters(pageNoint,pageSizeint,Integer.parseInt(courseId));
        mav.setViewName("back_courseReourceSet");
        mav.addObject("courseId",courseId);
        mav.addObject("chapterPageInfo",chapterPageInfo);
        return mav;
    }

    @ResponseBody
    @RequestMapping("/modifyStatus")
    public ResponseResult modifyStatus(HttpServletRequest request){
        ResponseResult rs=new ResponseResult();
        String chapterId=request.getParameter("chapterId");
        String status=request.getParameter("status");
        Chapter chapter=new Chapter();
        if (status.equals("1")){
            chapter.setStatus(0);
        }
        else if (status.equals("0")){
            chapter.setStatus(1);
        }
        chapter.setId(Integer.parseInt(chapterId));
        backChapterService.modifyStatus(chapter);
        rs.setMessage("状态修改成功");
        rs.setStatus(Constant.RESPONSE_SUCCESS);
        return rs;
    }

    @ResponseBody
    @RequestMapping("/selectChapter")
    public Chapter selectChapter(HttpServletRequest request){
        String chapterId=request.getParameter("chapterId");
        Chapter chapter=backChapterService.selectChapter(Integer.parseInt(chapterId));
        return chapter;
    }

    @ResponseBody
    @RequestMapping("/modifyChapter")
    public ResponseResult modifyChapter(HttpServletRequest request){
        ResponseResult rs=new ResponseResult();
        String chapterId=request.getParameter("chapterId");
        String title=request.getParameter("title");
        String info=request.getParameter("info");
        Chapter chapter=new Chapter();
        chapter.setId(Integer.parseInt(chapterId));
        chapter.setTitle(title);
        chapter.setInfo(info);
        backChapterService.modifyChapter(chapter);
        rs.setMessage("修改成功");
        rs.setStatus(Constant.RESPONSE_SUCCESS);
        return rs;
    }

    @ResponseBody
    @RequestMapping("/searchChapters")
    public ModelAndView searchChapters(HttpServletRequest request){
        ModelAndView mav=new ModelAndView();
        String title=request.getParameter("title");
        String status=request.getParameter("status");
        String courseId=request.getParameter("courseId");
        String id=request.getParameter("id");
        String dateStart=request.getParameter("dateStart");
        String dateEnd=request.getParameter("dateEnd");
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        if (ContentIsNull.isNull(dateStart)){
            dateStart="2020-05-01 00:00:00";
        }
        if (ContentIsNull.isNull(dateEnd)){
            dateEnd=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
        }
        String pageNo=request.getParameter("pageNo");
        String pageSize=request.getParameter("pageSize");
        //如果前端没有传递，使用默认值
        if (ContentIsNull.isNull(pageSize)) {
            pageSize= Constant.Chapter_DEFAULT_PAGE_SIZE;
        }
        if (ContentIsNull.isNull(pageNo)){
            pageNo= Constant.Chapter_DEFAULT_PAGE_NO;
        }
        int pageNoint=Integer.parseInt(pageNo);
        int pageSizeint=Integer.parseInt(pageSize);
        if (status.equals("-1")){
            status=null;
        }
        Date dateStart2=null;
        Date dateEnd2=null;
        try {
             dateStart2=simpleDateFormat.parse(dateStart);
             dateEnd2=simpleDateFormat.parse(dateEnd);

        } catch (ParseException e) {
            e.printStackTrace();
        }
        PageInfo<Chapter> chapterSearchPageInfo=backChapterService.searchChapters(title,status,id,courseId,dateStart2,dateEnd2,pageNoint,pageSizeint);

        mav.setViewName("back_chapterSearch_result");
        mav.addObject("chapterSearchPageInfo",chapterSearchPageInfo);
        return  mav;
    }

}
